package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.ccl.iot.App;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.R;
import com.ccl.iot.privates.BitmapCollector;
import com.ccl.iot.privates.BitmapMask;
import com.ccl.iot.publics.UIUtils;

public class RGBLightIconProvider extends DeviceIconProvider {
	/*
	 * 	Static functions
	 */
	public static boolean RegisterProviderType(){
		return RegisterProviderType(RGBLightIconProvider.class);
	}
	
	/*public synchronized static Bitmap Build(int aStyle, int aWidth, int aHeight, int aHighLightColor, int aBackColor, BitmapMask aMask){
		if (aWidth > 0 && aHeight > 0){
			Bitmap tIcon;
			String tKey = "Dimmer." + aStyle + "." + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode());
			
			tIcon = BitmapCollector.Get(tKey);
			
			if (tIcon == null){
				tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);
				Canvas tCanvas = new Canvas(tIcon);
				Paint tPaint = new Paint();
				float tRefLen = 0, tCenterX = 0, tCenterY = 0, tXOffset = 0, tYOffset = 0;
				
				if (aWidth > aHeight){
					tCenterY = tRefLen = aHeight * 0.5f;
					tXOffset = (aWidth - aHeight) * 0.5f;
					tCenterX = tCenterY + tXOffset;
				}else{
					tCenterX = tRefLen = aWidth * 0.5f;
					tYOffset = ((aHeight - aWidth) * 0.5f);
					tCenterY = tCenterX + tYOffset;
				}
				
				if (aBackColor != Color.TRANSPARENT){
					tCanvas.drawColor(aBackColor);
				}
				
				tPaint.setAntiAlias(true);
				tPaint.setFilterBitmap(true);
							
				tPaint.setColor(aHighLightColor);
				tPaint.setStyle(Paint.Style.FILL);
				
				DrawIconBase(tCanvas, aStyle, aBackColor, aHighLightColor);
	
				Path tPath = new Path();
				tPath.moveTo(tRefLen * 0.845f + tXOffset, tRefLen * 0.453f + tYOffset);
				tPath.arcTo(new RectF(tRefLen * 0.405f + tXOffset, tRefLen * 0.433f + tYOffset, tRefLen * 1.594f + tXOffset, tRefLen * 1.622f + tYOffset), -105, -330);
				tPath.lineTo(tRefLen * 1.155f + tXOffset, tRefLen * 0.615f + tYOffset);
				tPath.arcTo(new RectF(tRefLen * 0.559f + tXOffset, tRefLen * 0.586f + tYOffset, tRefLen * 1.441f + tXOffset, tRefLen * 1.468f + tYOffset), -69, 319);
				tPath.close();
				
				tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
				tPaint.setColor(aHighLightColor);
				tCanvas.drawPath(tPath, tPaint);
				
				tCanvas.drawRect(new RectF(tRefLen * 0.924f + tXOffset, tRefLen * 0.332f + tYOffset, tRefLen * 1.076f + tXOffset, tRefLen * 0.705f + tYOffset), tPaint);
				
				tCanvas.drawCircle(tCenterX, tRefLen * 0.332f + tYOffset, tRefLen * 0.076f, tPaint);
				tCanvas.drawCircle(tCenterX, tRefLen * 0.705f + tYOffset, tRefLen * 0.076f, tPaint);
				
				if (aMask != null){
					aMask.paintMask(tCanvas);
				}
				
				tCanvas.save();
				tCanvas.restore();
				
				BitmapCollector.Add(tKey, tIcon);
			}
			
			return tIcon;
		}
		
		return null;
	}*/
	
	
	
	//=========================================modify start=====================================================
	public synchronized static Bitmap Build(int aStyle, int aWidth, int aHeight, int aHighLightColor, int aBackColor, boolean isOn, boolean isPressed, BitmapMask aMask, int iconId){
		if (aWidth > 0 && aHeight > 0){
			Bitmap tIcon;
			String tKey = "Dimmer." + aStyle + "." + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode())+ "."+iconId;
			
			tIcon = BitmapCollector.Get(tKey);
			
			if (tIcon == null){
				tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);
				
				Canvas tCanvas = new Canvas(tIcon);
				
				Bitmap centerIcon = null;
				
				// TODO 设置意向控制调光器的图标

                if (iconId >= ICON_LIST_SWITCH_ON.length){
                    iconId = 0;
                }

                if(isOn){
                    centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_SWITCH_ON[iconId]);
                }else{
                    centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_SWITCH_OFF[iconId]);
                }



				
				int size = aWidth > aHeight ? aHeight : aWidth;
				
				centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);
				
				float left = (aWidth-centerIcon.getWidth())/2;
				float top = (aHeight-centerIcon.getHeight())/2;
				
				
				if (aBackColor != Color.TRANSPARENT){
					tCanvas.drawColor(aBackColor);
				}
				
				tCanvas.drawBitmap(centerIcon, left, top, new Paint());
				
				if (aMask != null){
					aMask.paintMask(tCanvas);
				}/*else{
					Paint paint = new Paint();
					paint.setAntiAlias(true);
					paint.setFilterBitmap(true);
					paint.setColor(Color.RED);
					float x = (float)(size*0.4)/2;
					tCanvas.drawCircle(left+x, top+x, x, paint);
				}*/
				
				tCanvas.save();
				tCanvas.restore();
				
				BitmapCollector.Add(tKey, tIcon);
				
				return tIcon;
			}
			
			return tIcon;
		}
		
		return null;
	}
	
	public static Bitmap Build(BatchIntentionDevice aDevice, int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing){
		if (aDevice != null){
			return Build(aStyle, aWidth, aHeight, aBackColor, true, false, aPressing, IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_FULL, aDevice.getSplitIconId());
		}
		
		return null;
	}
	
	//=========================================modify end=====================================================
	
	
	
	public static int GetHighLigthColor(boolean aOnline, boolean aIsOn, boolean aPressing){
		int tColor = Color.DKGRAY;

		if (aOnline) {
			if (aIsOn) {
				tColor = Color.GREEN;
			} else {
				tColor = App.getContext().getResources().getColor(R.color.device_close_color);
			}
		}
		
		return tColor;
	}
	
	public static int GetHighLigthColor(IOTRgbLight aDevice, boolean aPressing){
		if (aDevice != null){
			return GetHighLigthColor(aDevice.isOnline(), aDevice.isOn(), aPressing);
		}
		
		return Color.TRANSPARENT;
	}
	
	public static Bitmap Build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aOnline, boolean aIsOn, boolean aPressing, int aAuth, int iconId){
		Bitmap tIcon = null;
		int tColor = GetHighLigthColor(aOnline, aIsOn, aPressing);

		tIcon = Build(aStyle, aWidth, aHeight, tColor, aBackColor, aIsOn, aPressing, null, iconId);

		return tIcon;
	}
	
	public static Bitmap Build(IOTRgbLight aDevice, int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing){
		if (aDevice != null){
			return Build(aStyle, aWidth, aHeight, aBackColor, aDevice.isOnline(), aDevice.isOn(), aPressing, aDevice.getAuth(), aDevice.getSplitIconId());
		}
		
		return null;
	}
	
	
	/*
	 * 	Non-static functions
	 */
	public RGBLightIconProvider(IOTRgbLight aDevice) {
		super(aDevice);
	}
	
	@Override
	public Bitmap build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing) {
		return Build((IOTRgbLight)iDevice, aStyle, aWidth, aHeight, aBackColor, aPressing);
	}

}
